//
//  WBNetworkManager+Extension.swift
//  Weibo
//
//  Created by yinbo on 2018/3/6.
//  Copyright © 2018年 yinbo. All rights reserved.
//

import Foundation
//MARK:-封装新浪微博的网络请求方法
extension WBNetworkManager{
    func statusList(since_id:Int64=0,max_id:Int64=0,completion:@escaping (_ list:[[String: AnyObject]]?, _ isSuccess: Bool)->()){
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params:[String:AnyObject] = ["since_id":"\(since_id)" as AnyObject,"max_id":"\(max_id>0 ? max_id-1 : 0)" as AnyObject]
        tokenRequest(URLString: urlString, parameters: params) { (json, issuccess) in
            print(json)
            let result = json?["statuses"] as? [[String:AnyObject]]
            completion(result,issuccess)
         }
    }
    
    //返回微博的未读数量
    func unreadCount(completion:@escaping (_ count:Int)->()){
        guard let uid = userAccount.uid else { return  }
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params  = ["uid":uid]
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]) { (json, isSuccess) in
            print(json)
            let dict = json as? [String:AnyObject]
            let count = dict?["status"] as? Int
            completion(count ?? 0)
        }
        
    }
}
//MARK:OAuth相关方法
extension WBNetworkManager{
    //加载AccessToken
    func loadAccessToken(code:String,completion:@escaping (_ isSuccess:Bool)->()){
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parmas = ["client_id":WBAppKey,
                      "client_secret":WBAppSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":WBRedirectURL]
//        tokenRequest(method: .POST, URLString: urlString, parameters: parmas as [String : AnyObject]) { (json, isSuccess) in
//            print(json)
//        }
        request(method: .POST, URLString: urlString, parameters: parmas as [String : AnyObject]) { (json, isSuccess) in
            
            self.userAccount.yy_modelSet(with: (json as? [String : AnyObject]) ??  [:])
             print(self.userAccount)
            
             self.loadUserInfo(completion: { (dict) in
                print(dict)
                //使用用户信息字典设置用户信息（昵称和头像地址）
                self.userAccount.yy_modelSet(with: dict)
                self.userAccount.saveAccount()
                //用户信息加载完成再完成回调
                completion(isSuccess)
            })
        }
        
    }
}
//MARK:用户信息
extension WBNetworkManager{
    func loadUserInfo(completion:@escaping (_ dict:[String:AnyObject])->()){
        //加载用户信息，应该在用户登录后立即执行
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = ["uid": uid]
        
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]) { (json, isSuccess) in
//            print(json)
            completion(json as? [String:AnyObject] ?? [:])
        }
    }
}
